package fr.bordeaux1.planingdesetudiants.view;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;

public class DisponibiliteToggleButton extends Canvas {
	
	private static final String DISPONIBLE_STRING = "Disponible";
	private static final String INDISPONIBLE_STRING = "Indisponible";
	private boolean indisponible;
	private int creneau;
	
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public DisponibiliteToggleButton(Composite parent, int creneau) {
		super(parent, SWT.NONE);
		this.creneau = creneau;
		addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent arg0) {
				paint(arg0);
			}
		});
	}
	
	public boolean isIndisponible(){
		return indisponible;
	}
	public void setIndisponible(boolean value){
		indisponible = value;
		redraw();
	}
	public void toggle(){
		indisponible = !indisponible;
		redraw();
	}
	
	private void paint(PaintEvent evt){
		if(indisponible){
			Point size = evt.gc.textExtent(INDISPONIBLE_STRING);
			setBackground(new Color(evt.display,new RGB(255, 0, 0)));
			evt.gc.drawText(INDISPONIBLE_STRING, (getSize().x-size.x)/2, (getSize().y-size.y)/2);
		}else{
			Point size = evt.gc.textExtent(DISPONIBLE_STRING);
			setBackground(new Color(evt.display,new RGB(0, 255, 0)));
			evt.gc.drawText(DISPONIBLE_STRING, (getSize().x-size.x)/2, (getSize().y-size.y)/2);
		}
	}

	public int getCreneau() {
		return creneau;
	}

	
}
